package demo.practice.competition;

public class P5846 {

    public int findMiddleIndex(int[] nums) {
        if (nums == null || nums.length < 2)
            return 0;
        int totall = 0;
        for (int num : nums) {
            totall += num;
        }


        int t1 = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i - 1 >= 0) {
                t1 += nums[i - 1];
            }
            //前面部分存放在t1中
            //后面部分通过计算得到
            int t2 = totall - nums[i] - t1;
            if (t1 == t2) {
                return i;
            }
        }

        return -1;
    }
}
